﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using Aspose.Words;
using Aspose.Words.Drawing;
using System.Security.Cryptography;
using System.Net.Http;
using IPBoss.Copyright.Entities;

namespace IPBoss.Copyright.UtilityHelper
{
    public class FileHelper
    {


        #region 检测指定目录是否存在
        /// <summary>
        /// 检测指定目录是否存在
        /// </summary>
        /// <param name="directoryPath">目录的绝对路径</param>        
        public static bool IsExistDirectory(string directoryPath)
        {
            return Directory.Exists(directoryPath);
        }
        #endregion



        #region 创建一个目录
        /// <summary>
        /// 创建一个目录
        /// </summary>
        /// <param name="directoryPath">目录的绝对路径</param>
        public static void CreateDirectory(string directoryPath)
        {
            //如果目录不存在则创建该目录
            if (!IsExistDirectory(directoryPath))
            {
                Directory.CreateDirectory(directoryPath);
            }
        }
        #endregion

        #region 检测指定文件是否存在
        /// <summary>
        /// 检测指定文件是否存在,如果存在则返回true。
        /// </summary>
        /// <param name="filePath">文件的绝对路径</param>        
        public static bool IsExistFile(string filePath)
        {
            return File.Exists(filePath);
        }
        #endregion
        /// <summary>
        /// 获取一个文件的长度,单位为Byte
        /// </summary>
        /// <param name="filePath">文件的绝对路径</param>        
        public static int GetFileSize(string filePath)
        {
            //创建一个文件对象
            FileInfo fi = new FileInfo(filePath);

            //获取文件的大小
            return (int)fi.Length;
        }
        /// <summary>
        /// 同步下载文件
        /// </summary>
        /// <param name="url"></param>
        /// <param name="fileDownPath"></param>
        /// <returns></returns>
        public static bool DownLoad(string url, string fileDownPath)
        {
            try
            {
               
                var web = new WebClient();
                web.Credentials = CredentialCache.DefaultCredentials;
                web.Headers.Add(HttpRequestHeader.UserAgent, "anything");
                web.DownloadFile(url, fileDownPath);
                web.Dispose();
                if (GetFileSize(fileDownPath) > 0 && IsExistFile(fileDownPath))
                {
                    return true;
                }
                else
                {

                    return false;
                }
                

            }
            catch (Exception ex)
            {
                Log4NetHelper.Error("FileHelper", "DownLoad:" + ex.Message);
                return false;
            }


        }


        public static void DownloadFile(string FilePath, string FileDownPath)
        {
            string functionName = "DownloadFile";           
            try
            {

                using (var web = new WebClient())
                {
                    web.DownloadFile(FilePath, FileDownPath);
                }
            }
            catch (Exception ex)
            {
                Log4NetHelper.Error(functionName + ":" + ex.Message, "FilePath:" + FilePath + " FileDownPath:" + FileDownPath, ex);

            }


        }

        #region 从文件的绝对路径中获取文件名( 包含扩展名,支持七牛云虚拟路径 )
        /// <summary>
        /// 支持七牛或普通地址
        /// </summary>
        /// <param name="strUri"></param>
        /// <returns></returns>
        public static string GetNewFileName(string strUri)
        {
            //eg:?domain=crm-contract&key=Contract/Product/BrandIdentityCard_P4081794.jpg
            if (strUri.Length > 7 && strUri.Substring(0, 7).ToLower() == "?domain")
            {
                string url = strUri.Split('=')[2].ToString();
                return Path.GetFileName(url);
            }
            else
            {
                return Path.GetFileName(strUri);
            }
        }
        #endregion


        /// <summary>
        /// 判断是否pdf
        /// </summary>
        /// <param name="FilePath"></param>
        /// <returns></returns>
        public static bool IsPDF(string FilePath)
        {
            if (Path.GetExtension(FilePath).ToUpper() == ".PDF")
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 下载文件到本地
        /// </summary>
        /// <param name="FilePath">文件路径</param>
        /// <param name="ViewModel"></param>
        /// <returns></returns>
        public static string GetServerFile(CopyRightCertViewModel model)
        {


            string strUri = model.VirtualPath;
            if (strUri.Length > 7 && strUri.Substring(0, 7).ToLower() == "?domain")
            {
                QinniuFileHelper qinniuFileHelper = new QinniuFileHelper();
                strUri = qinniuFileHelper.CreatePrivateUrl(strUri);
            }
            else
            {
                strUri = ConfigHelper.GetAppSetting("VirtualBasePath") + strUri;
            }
            return strUri;
        }

        public static bool DownloadServerFile(string FilePath, CopyRightCertViewModel model)
        {

            if (!String.IsNullOrEmpty(FilePath))
            {
                return FileHelper.DownLoad(GetServerFile(model), FilePath);

            }
            return false;
        }
    }
}
